﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExtendPropertyLib.WPF;
using ExtendPropertyLib;
using MaxZhang.EasyEntities.Persistence.Provider;
using System.IO;
using ExORMDesginer.Helper;
using DatabaseSchemaReader.DataSchema;
namespace ExORMDesginer.UserControls
{
    public class SQLServerDialogInfo : BusinessInfoBase<SQLServerDialogInfo>
    {
        public override void  OnDoCreate(ExtendObject item, params object[] args)
        {
 	        base.OnDoCreate(item, args);
            this.LoadProperties();
            this.SQLType = SqlType.SqlServer;
        }


        public string GetConnectionString()
        {
            return string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};pwd={3};",ServerName,DatabaseName,UserName,Password);
        }
        

        public void Connect()
        {
            SqlProvider provider = new SqlProvider(GetConnectionString());
            provider.DbConnection.Open();
        }

        private void InitDir()
        {
            string AppDataPath = Path.Combine(basePath, "AppData");
            if (!Directory.Exists(AppDataPath))
                Directory.CreateDirectory(AppDataPath);
        }

        public void SaveProperties()
        {
            InitDir();
            File.WriteAllText(saveFileInfo, this.Serializer());
        }


        public void LoadProperties()
        {
            InitDir();
            if (File.Exists(saveFileInfo))
            {
                string content = File.ReadAllText(saveFileInfo);
                if (!string.IsNullOrEmpty(content))
                {
                    var info = File.ReadAllText(saveFileInfo).Deserialize<SQLServerDialogInfo>();
                    if (info != null)
                    {
                        this.UserName = info.UserName;
                        this.Password = info.Password;
                        this.DatabaseName = info.DatabaseName;
                        this.ServerName = info.ServerName;
                    }
                }
            }
          
        }
        
        private static string basePath = AppDomain.CurrentDomain.BaseDirectory;
        private static string AppDataPath = Path.Combine(basePath, "AppData");
        private static string saveFileInfo = Path.Combine(AppDataPath, "Connection.xml");

        public static ExtendProperty SQLTypeProperty = RegisterProperty(v => v.SQLType);
        /// <summary>
        /// SQL数据库类型
        /// </summary>
        public SqlType SQLType { set { SetValue(SQLTypeProperty, value); } get { return (SqlType)GetValue(SQLTypeProperty); } }

        #region Display Data

        public static ExtendProperty ServerNameProperty = RegisterProperty(v => v.ServerName);
        public string ServerName { set { SetValue(ServerNameProperty, value); } get { return (string)GetValue(ServerNameProperty); } }


        public static ExtendProperty UserNameProperty = RegisterProperty(v => v.UserName);
        public string UserName { set { SetValue(UserNameProperty, value); } get { return (string)GetValue(UserNameProperty); } }


        public static ExtendProperty PasswordProperty = RegisterProperty(v => v.Password);
        public string Password { set { SetValue(PasswordProperty, value); } get { return (string)GetValue(PasswordProperty); } }


        public static ExtendProperty DatabaseNameProperty = RegisterProperty(v => v.DatabaseName);
        public string DatabaseName { set { SetValue(DatabaseNameProperty, value); } get { return (string)GetValue(DatabaseNameProperty); } }

        #endregion

        #region valiation

        protected override void AddValidationRules()
        {
            ValiationRules.Add(ServerNameProperty, () => {
                string result = null;

                if (string.IsNullOrEmpty(ServerName))
                {
                    result = "服务器名称不能为空！";
                }
                return result;
            
            });

            ValiationRules.Add(UserNameProperty, () =>
            {
                string result = null;

                if (string.IsNullOrEmpty(UserName))
                {
                    result = "用户名不能为空！";
                }
                return result;

            });

            ValiationRules.Add(PasswordProperty, () =>
            {
                string result = null;

                if (string.IsNullOrEmpty(Password))
                {
                    result = "密码不能为空！";
                }
                return result;

            });

            ValiationRules.Add(DatabaseNameProperty, () =>
            {
                string result = null;

                if (string.IsNullOrEmpty(DatabaseName))
                {
                    result = "数据库名称不能为空！";
                }
                return result;

            });

        }


        #endregion



    }
}   
